﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class ProductCategoryObject
    {
        public static ProductCategory[] GetProductCategories()
        {
            return Query(null);
        }


        public static ProductCategory GetProductCategory(int id)
        {
            ProductCategory productCategory = new ProductCategory();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(ProductCategoryDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = ProductCategoryDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable productCategories = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            productCategory.Id                        = Convert.ToInt32(productCategories.Rows[0][ProductCategoryDesc.Id]);
            productCategory.ProductCategoryId         = Convert.ToInt32(productCategories.Rows[0][ProductCategoryDesc.ProductCategoryId]);
            productCategory.FactoryId                 = Convert.ToInt32(productCategories.Rows[0][ProductCategoryDesc.FactoryId]);
            productCategory.UserDefinedId             = Convert.ToString(productCategories.Rows[0][ProductCategoryDesc.UserDefinedId]);
            productCategory.Name                      = Convert.ToString(productCategories.Rows[0][ProductCategoryDesc.Name]);
            productCategory.Discount                  = Convert.ToInt32(productCategories.Rows[0][ProductCategoryDesc.Discount]);
           
            return productCategory;
        }

        public static ProductCategory GetProductCategory(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(ProductCategoryDesc.Name, ConditionExpress.EQUALS, name);

            ProductCategory[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static ProductCategory[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + ProductCategoryDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                ProductCategory[] productCategories = new ProductCategory[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ProductCategory productCategory = new ProductCategory();

                    productCategory.Id                           = Convert.ToInt32(table.Rows[i][ProductCategoryDesc.Id]);
                    productCategory.ProductCategoryId            = Convert.ToInt32(table.Rows[i][ProductCategoryDesc.ProductCategoryId]);
                    productCategory.FactoryId                    = Convert.ToInt32(table.Rows[i][ProductCategoryDesc.FactoryId]);
                    productCategory.UserDefinedId                = Convert.ToString(table.Rows[i][ProductCategoryDesc.UserDefinedId]);
                    productCategory.Name                         = Convert.ToString(table.Rows[i][ProductCategoryDesc.Name]);
                    productCategory.Discount                     = Convert.ToInt32(table.Rows[i][ProductCategoryDesc.Discount]);

                    productCategories.SetValue(productCategory, i);
                }

                return productCategories;
            }
            return null;
        }

        public static Int32 Add(ProductCategory productCategory)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(ProductCategoryDesc.ProductCategoryId,            productCategory.ProductCategoryId);
            builder.InsertAdd(ProductCategoryDesc.FactoryId,                    productCategory.FactoryId);
            builder.InsertAdd(ProductCategoryDesc.UserDefinedId,                productCategory.UserDefinedId);
            builder.InsertAdd(ProductCategoryDesc.Name,                         productCategory.Name);
            builder.InsertAdd(ProductCategoryDesc.Discount,                     productCategory.Discount);
            builder.QueryTableName = ProductCategoryDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(ProductCategoryDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = ProductCategoryDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(ProductCategory productCategory)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(ProductCategoryDesc.ProductCategoryId,                  productCategory.ProductCategoryId);
            builder.UpdateAdd(ProductCategoryDesc.FactoryId,                          productCategory.FactoryId);
            builder.UpdateAdd(ProductCategoryDesc.UserDefinedId,                      productCategory.UserDefinedId);
            builder.UpdateAdd(ProductCategoryDesc.Name,                               productCategory.Name);
            builder.UpdateAdd(ProductCategoryDesc.Discount,                           productCategory.Discount);
            builder.ConditionAdd(ProductCategoryDesc.Id, ConditionExpress.EQUALS,     productCategory.Id);
            builder.QueryTableName = ProductCategoryDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
